//
//  WYMomentOperationMoreView.m
//  FriendCircleDemo
//
//  Created by mac on 2018/10/8.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentOperationMoreView.h"
#import "UIImage+WYExtension.h"
#import "WYMomentOperationMoreItemView.h"

/// Hide Notification
static NSString * const WYMomentOperationMoreViewHideNotification = @"WYMomentOperationMoreViewHideNotification";
static NSString * const WYMomentOperationMoreViewHideUserInfoKey = @"WYMomentOperationMoreViewHideUserInfoKey";

@interface WYMomentOperationMoreView ()
/// 点赞
@property (nonatomic, readwrite, weak) WYMomentOperationMoreItemView *attitudesBtn;
/// 评论
@property (nonatomic, readwrite, weak) WYMomentOperationMoreItemView *commentBtn;
/// 分割线
@property (nonatomic, readwrite, weak) UIImageView *divider;
/// 是否已显示
@property (nonatomic, readwrite, assign) BOOL isShow;
@end

@implementation WYMomentOperationMoreView

+ (instancetype)operationMoreView
{
    return [[self alloc] init];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        /// 设置能交互
        self.userInteractionEnabled = YES;
        self.image = [UIImage wy_resizableImage:@"wx_albumOperateMoreViewBkg_40x39"];
        /// 当宽度为0的时候 子控件也消失
        self.clipsToBounds = YES;
        /// 高亮状态下的背景色
        /// 评论
        UIImage * cBackgroundImage = [UIImage wy_resizableImage:@"wx_albumCommentBackgroundHL_15x39"];
        /// 赞
        UIImage * aBackgroundImage = [UIImage wy_resizableImage:@"wx_albumLikeBackgroundHL_15x39"];
        
        /// 设置点赞按钮
        WYMomentOperationMoreItemView *attitudesBtn = [WYMomentOperationMoreItemView buttonWithType:UIButtonTypeCustom];
        [attitudesBtn setTitle:@"赞" forState:UIControlStateNormal];
        
        [attitudesBtn setImage:[UIImage imageNamed:@"wx_albumLike_20x20"] forState:UIControlStateNormal];
        [attitudesBtn setImage:[UIImage imageNamed:@"wx_albumLikeHL_20x20"] forState:UIControlStateHighlighted];
        [attitudesBtn setBackgroundImage:aBackgroundImage forState:UIControlStateHighlighted];
        [attitudesBtn addTarget:self action:@selector(attitudesBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        /// 开启点击动画
        attitudesBtn.allowAnimationWhenClick = YES;
        self.attitudesBtn = attitudesBtn;
        [self addSubview:attitudesBtn];
        
        /// 设置分割线
        UIImageView *divider = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wx_albumCommentLine_0x24"]];
        self.divider = divider;
        [self addSubview:divider];
        
        /// 设置评论按钮
        WYMomentOperationMoreItemView *commentBtn = [WYMomentOperationMoreItemView buttonWithType:UIButtonTypeCustom];
        [commentBtn setTitle:@"评论" forState:UIControlStateNormal];
        [commentBtn setImage:[UIImage imageNamed:@"wx_albumCommentSingleA_20x20"] forState:UIControlStateNormal];
        [commentBtn setImage:[UIImage imageNamed:@"wx_albumCommentSingleAHL_20x20"] forState:UIControlStateHighlighted];
        [commentBtn setBackgroundImage:cBackgroundImage forState:UIControlStateHighlighted];
        [commentBtn addTarget:self action:@selector(commentBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.commentBtn = commentBtn;
        [self addSubview:commentBtn];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moreViewHideNotification:) name:WYMomentOperationMoreViewHideNotification object:nil];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    /// 固定高度
    frame.size.height = WYMomentOperationMoreViewHeight;
    [super setFrame:frame];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    /// 布局赞
    CGFloat attitudesBtnW = (self.width - self.divider.width)*.5f;
    CGFloat attitudesBtnH = self.height;
    self.attitudesBtn.size = CGSizeMake(attitudesBtnW, attitudesBtnH);
    
    
    /// 布局分割线
    self.divider.left = CGRectGetMaxX(self.attitudesBtn.frame);
    self.divider.centerY = self.height*.5f;
    
    
    /// 布局评论
    CGFloat commentBtnX = CGRectGetMaxX(self.divider.frame);
    CGFloat commentBtnW = attitudesBtnW;
    CGFloat commentBtnH = attitudesBtnH;
    self.commentBtn.frame = CGRectMake(commentBtnX, 0, commentBtnW, commentBtnH);
    
}

//隐藏通知
-(void)moreViewHideNotification:(NSNotification *)note
{
    BOOL animated = [note.userInfo[WYMomentOperationMoreViewHideUserInfoKey] boolValue];
    [self hideWithAnimated:animated];
}

#pragma mark - Show Or Hide

- (void)showWithAnimated:(BOOL)animated
{
    if (self.isShow) return;
    
    /// 隐藏之前显示的所有的MoreView
    [[self class] hideAllOperationMoreViewWithAnimated:YES];
    
    /// 置为Yes
    self.isShow = YES;
    
    if (!animated) {
        self.width = WYMomentOperationMoreViewWidth;
        self.left = self.left-self.width;
        return;
    }
    
    /// 动画
    [UIView animateWithDuration:WYMommentAnimatedDuration delay:0 usingSpringWithDamping:.7f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.width = WYMomentOperationMoreViewWidth;
        self.left = self.left-self.width;
        /// 强制布局
        
    } completion:^(BOOL finished) {
    }];
}

- (void)hideWithAnimated:(BOOL)animated
{
    [self hideAnimated:animated afterDelay:0];
}

- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay
{
    if (!self.isShow) return;
    
    self.isShow = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (!animated) {
            // 无动画
            self.left = self.left + self.width;
            self.width = 0;
            self.isShow = NO;
            return ;
        }
        
        /// 动画
        [UIView animateWithDuration:WYMommentAnimatedDuration delay:0 usingSpringWithDamping:1.0f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.left = self.left + self.width;
            self.width = 0;
            /// 强制布局
            
        } completion:^(BOOL finished) {
        }];
        
    });
    
}


/// 隐藏所有操作Menu
+ (void)hideAllOperationMoreViewWithAnimated:(BOOL)animated;{
    /// 发布通知
    [[NSNotificationCenter defaultCenter] postNotificationName:WYMomentOperationMoreViewHideNotification object:nil userInfo:@{WYMomentOperationMoreViewHideUserInfoKey:@(animated)}];
}

//点赞点击事件
-(void)attitudesBtnAction:(UIButton *)btn
{
    !self.attitudesClickedCallback?:self.attitudesClickedCallback(self);
}

-(void)commentBtnAction:(UIButton *)btn
{
    !self.commentClickedCallback?:self.commentClickedCallback(self);
}

-(void)setItemViewModel:(WYMomentItemViewModel *)itemViewModel
{
    _itemViewModel = itemViewModel;
    [self.attitudesBtn setTitle:itemViewModel.moment.attitudesStatus==0?@"赞":@"取消" forState:UIControlStateNormal];
}
@end
